package com.groot.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.groot.common.util.MedicalDBUtil;
import com.groot.medical.entity.Role;
import com.groot.medical.entity.RoleMenu;
import com.groot.medical.entity.SysUser;
import com.groot.medical.entity.UserRole;

public class RoleDaoImpl implements RoleDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	
	@Override
	public int addRole(Role role) {
		int i =0;
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			String sql="insert into t_role(role_id,role_name,role_describe)values(?,?,?)";
			ps=conn.prepareStatement(sql);
			ps.setString(1,role.getRoleId());
			ps.setString(2, role.getRoleName());
			ps.setString(3, role.getRoleDescribe());
			i= ps.executeUpdate();
			
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
			return i;
			
	}

	@Override
	public List<Role> findAllRoleList() {
		try {
			conn = MedicalDBUtil.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		List<Role> roleList=new ArrayList<Role>();//创建list集合，用于保持User对象
		try {
		conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
		String sql="select * from t_role";
		ps=conn.prepareStatement(sql);
		rs=ps.executeQuery();
		while(rs.next()){
		Role role=new Role();//创建User对象用于保持从数据看查出来的数据
		role.setRoleId(rs.getString(1));
		role.setRoleName(rs.getString(2));
		role.setRoleDescribe(rs.getString(3));
		
		roleList.add(role);
		}
		} catch (SQLException e) {

		e.printStackTrace();
		}
		finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
		return roleList;
	}

	@Override
	public void addRoleAndMenu(RoleMenu roleMenu) {
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			
			String[] split = roleMenu.getMenuId().split(",");
			for(int i=0;i<split.length;i++)
			{
			String sql="insert into t_role_menu(menu_id,role_id)values(?,?)";
			ps=conn.prepareStatement(sql);
			ps.setString(1, split[i]);
			ps.setString(2, roleMenu.getRoleId());
			ps.executeUpdate();
			}
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
		
	}

	@Override
	public List<UserRole> findRoleByUserId(String userid) {
		List<UserRole> UrList = new ArrayList<>();
		try {
			conn = MedicalDBUtil.getConnection();
			String sql = "select * from t_user_role where user_id =?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, userid);
			rs = ps.executeQuery();
			while(rs.next()){ 
				UserRole userRole = new UserRole();
				userRole.setUserId(rs.getString(1));
				userRole.setRoleId(rs.getString(2));
				
				 UrList.add(userRole);
				 
				}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			MedicalDBUtil.closeAll(conn, ps, rs);
		}
		return UrList;
	}

	@Override
	public Role findRoleByRoleId(String roleid) {
		
		try {
			conn = MedicalDBUtil.getConnection();
			String sql = "select * from t_role where role_id =?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, roleid);
			rs = ps.executeQuery();
			while(rs.next()){ 
				Role role = new Role();
				role.setRoleId(rs.getString(1));
				role.setRoleName(rs.getString(2));
				role.setRoleDescribe(rs.getString(3));
				return role;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
			return null;
			}

	@Override
	public int editRole(Role role) {
		int i =0;
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			//UPDATE student SET name='"+name+"', age=" + age+" where name='大红'";
			String sql="update t_role set role_name=?,role_describe=? where role_id=?";
			ps=conn.prepareStatement(sql);
			ps.setString(1, role.getRoleName());
			ps.setString(2, role.getRoleDescribe());
			ps.setString(3,role.getRoleId());
			i= ps.executeUpdate();
			
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
			return i;
	}

	@Override
	public int deleteRoleById(String id) {
		int i=0;
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			String sql="delete from t_role where role_id=?";
			ps=conn.prepareStatement(sql);
			ps.setString(1, id);
			i=ps.executeUpdate();
			
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
		return i;
	}

	@Override
	public void deleteRoleAndMenuByRoleId(String id) {
		try {
			conn=MedicalDBUtil.getConnection();//通过工具类直接调用getConnection方法获取jdbc连接。
			String sql="delete from t_role_menu where role_id=?";
			ps=conn.prepareStatement(sql);
			ps.setString(1, id);
			ps.executeUpdate();
			
			} catch (SQLException e) {

			e.printStackTrace();
			}finally{
				MedicalDBUtil.closeAll(conn, ps, rs);
			}
		
	}

	@Override
	public int checkRoleHad(String id) {
    int personCounts=0;
		

	StringBuffer sql = new StringBuffer("SELECT COUNT(role_id) id FROM t_user_role where  role_id='"+id+"'");

        //查询结果
        try {
        	 conn = MedicalDBUtil.getConnection();
             ps = conn.prepareStatement(sql.toString());
             rs = ps.executeQuery();
            if(rs.next()) {
            	personCounts = rs.getInt("id");
            }
        } catch (SQLException e) {

    		e.printStackTrace();
    		}
    		finally{
    			MedicalDBUtil.closeAll(conn, ps, rs);
    		}
        return personCounts;
	}

}
